var app = new Vue({
	el: '#app',
	delimiters: ['[[', ']]'], //修改vue读取变量的语法 {{}}改为[[]]
	data: {
		host: host,
		//v-model
		username: '',
		password: '',
		password2: '',
		mobile: '',
		email: '',
		allow: '',
		image_code: '',
		image_code_url: '',
		uuid: '',
		sms_code: '',
		send_info: false, //类比上厕所 send_info是锁，false为开门

		//v-show
		error_name: false,
		error_password: false,
		error_check_password: false,
		error_phone: false,
		error_email: false,
		error_image_code: false,
		error_sms_code: false,
		error_allow: false,


		//错误信息
		//error_name_message
		error_name_message: '',
		error_mobile_messgae: '',
		error_email_messgae: '',
		error_image_code_message: '',
		sms_code_tip: '获取短信验证码',
		error_sms_code_message: '',



	},
	mounted() { //页面加载完调用
		this.generate_image_code();

	},
	methods: { //定义实现事件方法
		generate_image_code: function () {
			let that = this
			that.uuid = generateUUID();
			axios.get(that.host + '/imgecode/' + that.uuid)
				.then(function (resp) {
					// console.log(resp.data);
					console.log('sss');
					that.image_code_url = that.host + '/imgecode/' + that.uuid + '/';
					console.log('vvv');
				})


		},

		//校验用户名
		check_username: function () {
			let that = this
			var re = /^[a-zA-Z0-9_-]{5,20}$/;
			if (re.test(that.username)) {
				that.error_name = false;
				axios.get(that.host + '/usernames/' + that.username + '/count')
					.then(function (resp) {
						if (resp.data.count > 0) {
							// console.log(resp.data.count);
							that.error_name_message = '用户名重复';
							that.error_name = true
						} else {
							// console.log(resp.data.errmsg);
							that.error_name = false
						}
					})
			} else {
				that.error_name_message = '请输入5-20位有效用户名';
				that.error_name = true
			}
		},
		//校验邮箱
		check_email: function () {
			let that = this
			var re = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
			if (re.test(that.email)) {
				that.error_email = false;
				axios.get(that.host + '/email/' + that.email + '/count')
					.then(function (resp) {
						if (resp.data.count > 0) {
							console.log(resp.data.count);
							that.error_email_messgae = '邮箱重复';
							that.error_email = true
						} else {
							// console.log(resp.data.errmsg);
							that.error_email = false
						}
					})
			} else {
				that.error_email_message = '请输入有效邮箱';
				that.error_email = true
			}
		},
		//校验密码
		check_pwd: function () {
			var re = /^[0-9A-Za-z]{8,20}$/;
			if (re.test(this.password)) {
				this.error_password = false;
			} else {
				this.error_password = true;
			}

		},
		//校验重密码
		check_cpwd: function () {
			if (this.password == this.password2) {
				this.error_check_password = false;
			} else {
				this.error_check_password = true;
			}

		},
		//校验手机号
		check_phone: function () {
			let that = this
			var re = /^1[3-9]\d{9}$/;
			if (re.test(that.mobile)) {
				axios.get(that.host + '/mobiles/' + that.mobile + '/count')
					.then(function (resp) {
						if (resp.data.count > 0) {
							that.error_phone = true;
							that.error_mobile_messgae = '手机号已注册';
						} else {
							that.error_phone = false;
						}
					})

			} else {
				that.error_mobile_messgae = '您输入的手机号码格式不正确';
				that.error_phone = true;
			}
		},
		//校验图片验证码
		check_image_code: function () {
			if (this.image_code.length != 4) {
				this.error_image_code_message = '请输入正确的验证码'
				this.error_image_code = true;
			} else {
				this.error_image_code = false;

			}
		},
		// //校验短信验证码
		// check_sms_code: function() {
		// 	if (this.sms_code.length != 6) {
		// 		this.error_sms_code_message = '请输入短信驗證碼'
		// 		this.error_sms_code = true;
		// 	} else {
		// 		this.error_sms_code = false;
		// 	}
		// },
		// //发送短信验证码
		// send_sms_code: function() {
		// 	let that = this
		// 	if (that.send_info == true) { //判断是否门为关
		// 		return; //如果关，返回
		// 	}
		// 	that.send_info = true; //第一次进来，关门

		// 	that.check_phone();
		// 	that.check_image_code();
		// 	if (that.error_phone == true || that.error_image_code == true) {
		// 		that.send_info = false;
		// 		return;
		// 	}

		// 	axios.get(that.host + '/smscode/' + that.mobile + '/?uuid=' + that.uuid +
		// 			'&image_code=' + that.image_code, {
		// 				responseType: 'json'
		// 			})
		// 		.then(function(resp) {
		// 			if (resp.data.code == '0') {
		// 				let num = 60
		// 				//请求成功	倒计时\
		// 				//回调函数:每隔多长时间执行一次
		// 				// setInterval('回调函数','时间间隔')
		// 				var t = setInterval(function() {
		// 					if (num == 1) { //num等于1停止倒计时
		// 						clearInterval(t);
		// 						that.sms_code_tip = '获取短信验证码';
		// 						//重新生成验证码
		// 						that.generate_image_code();
		// 						that.send_info = false; //上完出去，开门
		// 					} else {
		// 						num -= 1 //每秒减一
		// 						that.sms_code_tip = num + '秒';
		// 					}

		// 				}, 1000) //1秒 1000毫秒

		// 			} else {
		// 				if (resp.data.code == '400') { //错误信息
		// 					that.error_image_code_message = resp.data.errmsg;
		// 					that.error_image_code = true;

		// 				}
		// 				that.send_info = false; //出不来开门
		// 			}
		// 		})
		// 		.catch(function(errmsg) {
		// 			console.log(errmsg.response);
		// 			// this.error_sms_code = true;
		// 			that.send_info = false;
		// 		})

		// },

		//校验勾选协议
		check_allow: function () {
			let that = this
			if (!that.allow) {
				that.error_allow = true;
			} else {
				that.error_allow = false
			}
		},
		//监听表单提交
		on_submit: function () {
			let that = this;
			that.check_username();
			that.check_pwd();
			that.check_cpwd();
			that.check_phone();
			that.check_image_code();
			that.check_email();
			// this.send_sms_code();
			that.check_allow();


			if (that.error_name == true || that.error_password == true || that.error_check_password ==
				true || that.error_phone == true || that.error_allow == true || that.error_image_code == true
				|| that.error_email == true) {
				//如果以上有一个错误提示则禁用表单提交按钮
				window.event.returnValue = false;
			} else {

				var url = that.host + '/register/'
				axios.post(url, {
					username: that.username,
					password: that.password,
					password2: that.password2,
					mobile: that.mobile,
					sms_code: that.sms_code,
					image_code: that.image_code,
					allow: that.allow,
					uuid: that.uuid,
					email: that.email,
				}, {
					responseType: 'json',
					//表示跨域请求是否需要使用凭证传递cookie信息
					withCredentials: true,

				})
					.then(response => {
						if (response.data.code == 200) {
							window.location.href = 'login.html';
						} else {
							if (response.data.code == 400) {
								that.error_image_code_message = response.data.error;
								that.error_image_code = true
							}
						}


					})
					.catch(error => {
						alert('内部错误，联系管理员')
					})




			}
		}
	},

})
